package xyz.fefine;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by feng_ on 2016/9/12.
 * 支持嵌套的
 */
public class NestedEnv implements Enviorment {

    private Enviorment ourter;
    private Map<String,Object> inner;

    public NestedEnv(Enviorment ourter) {
        inner = new HashMap<>();
        this.ourter = ourter;
    }

    @Override
    public void setOuter(Enviorment enviorment) {
        this.ourter = enviorment;
    }

    @Override
    public void put(String name, Object value) {
        Enviorment e = where(name);
        if (e == null) {
            e = this;
        }
        e.putNew(name, value);
    }

    @Override
    public void putNew(String name, Object val) {
        inner.put(name, val);
    }

    @Override
    public Object get(String name) {
        Object res = inner.get(name);
        if (res == null && ourter != null) {
            return ourter.get(name);
        }
        return res;
    }


    @Override
    public Enviorment where(String name) {
        Object r = inner.get(name);
        if (r != null) {
            return this;
        } else if (ourter == null) {
            return null;
        } else {
            return ourter.where(name);
        }
    }

    @Override
    public void put(int nest, int index, Object value) {

    }

    @Override
    public Object get(int nest, int index) {
        return null;
    }

    @Override
    public Symbols symbols() {
        return null;
    }

}
